package com.zs.zcerp.remark.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wuwenze.poi.util.DateUtil;
import com.zs.zcerp.common.entity.FebsConstant;
import com.zs.zcerp.common.entity.QueryRequest;
import com.zs.zcerp.common.utils.SortUtil;
import com.zs.zcerp.entrance.entity.EpMain;
import com.zs.zcerp.entrance.service.IEpMainService;
import com.zs.zcerp.remark.entity.LayUIDto;
import com.zs.zcerp.remark.entity.RemarkUser;
import com.zs.zcerp.remark.entity.TgPpsM;
import com.zs.zcerp.remark.mapper.TgPpsMMapper;
import com.zs.zcerp.remark.service.CommonService;
import com.zs.zcerp.remark.service.ITgPpsMService;
import com.zs.zcerp.system.entity.User;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.time.DateUtils;
import org.apache.ibatis.annotations.Insert;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
@DS("oracle2")
@RequiredArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class TgPpsMServiceImpl extends ServiceImpl<TgPpsMMapper, TgPpsM> implements ITgPpsMService {

    @Autowired
    private TgPpsMMapper tgPpsMMapper;

    @Autowired
    private CommonService commonService;

    @Resource
    private IEpMainService epMainService;

    @Override
    public TgPpsM findByPpsNo(String ppsNo) {
        return convertDetpIdAndEmpnm(tgPpsMMapper.queryByPpsNo(ppsNo));
    }

    @Override
    public LayUIDto findRemarkList(Integer tabId, String username, int pageNum, int pageSize) {
        List<TgPpsM> tgPpsMSList = tgPpsMMapper.queryMarked(tabId, username, (pageNum - 1) * pageSize, pageNum * pageSize);
        int total = tgPpsMMapper.countQueryMarked(tabId, username);
        for (TgPpsM tgPpsM: tgPpsMSList ) {
            convertDetpIdAndEmpnm(tgPpsM);
        }
        LayUIDto layUIDto = new LayUIDto();
        layUIDto.setCode(0);
        layUIDto.setCount(total);
        layUIDto.setMsg("success");
        layUIDto.setData(tgPpsMSList);
        return layUIDto;
    }

    @Override
    public IPage<TgPpsM> findRemarkLists(RemarkUser remarkUser, QueryRequest request) {
        Page<TgPpsM> page = new Page<>(request.getPageNum(), request.getPageSize());
        page.setSearchCount(false);
        page.setTotal(baseMapper.countSystemList(remarkUser));
        SortUtil.handlePageSort(request, page, "PPS_DATE", FebsConstant.ORDER_DESC, false);
        IPage<TgPpsM> pages = baseMapper.findSystemListPage(page, remarkUser);
        for (TgPpsM tgPpsM : pages.getRecords()) {
            convertDetpIdAndEmpnm(tgPpsM);
        }
        return pages;
    }

    @Override
    public IPage<TgPpsM> listProposal(RemarkUser remarkUser, QueryRequest request) {
        Page<TgPpsM> page = new Page<>(request.getPageNum(), request.getPageSize());
        page.setSearchCount(false);
        page.setTotal(baseMapper.countProposalList(remarkUser));
        SortUtil.handlePageSort(request, page, "PPS_DATE", FebsConstant.ORDER_DESC, false);
        IPage<TgPpsM> pages = baseMapper.listProposalPage(page, remarkUser);
        for (TgPpsM tgPpsM : pages.getRecords()) {
            convertDetpIdAndEmpnm(tgPpsM);
        }
        return pages;
    }

    @Override
    public boolean saveOrUpdate(TgPpsM tgPpsM) {
        User user =(User) SecurityUtils.getSubject().getPrincipal();
        EpMain epMain = epMainService.findByEmpnoActive(user.getUsername());
        tgPpsM.setLastUser(epMain.getEmpId());
        tgPpsM.setLastDate(new Date());
        if (tgPpsM.getPpsNo() == null) {
            tgPpsM.setOrgId(epMain.getOrgId());
            tgPpsM.setPpsNo(getPPSNO(epMain.getOrgId()));
            tgPpsM.setPpsDate(new Date());
            tgPpsM.setPpsEmpno(epMain.getEmpId());
            tgPpsM.setEmpnm(user.getCname());
            tgPpsM.setDeptNo(epMain.getDeptNo());
            tgPpsM.setWorkNo(epMain.getPostId());
            tgPpsM.setCreDate(DateUtil.format("yyyy/MM/dd", new Date()));
            tgPpsM.setGrtDept(epMain.getDeptNo());
            tgPpsM.setGrtUser(epMain.getEmpId());
            save(tgPpsM);
        } else {
            update(tgPpsM, new QueryWrapper<TgPpsM>().eq("PPS_NO", tgPpsM.getPpsNo()).eq("STATUS", 1));
        }
        return true;
    }

    @Override
    public String getPPSNO(Integer orgId) {
        return tgPpsMMapper.getPPSNO(orgId);
    }

    @Override
    public TgPpsM getByPpsNo(String ppsNo) {
        return getOne(new QueryWrapper<TgPpsM>().eq("PPS_NO", ppsNo));
    }

    // 转换部门id与员工id,以名字显示
    private TgPpsM convertDetpIdAndEmpnm(TgPpsM tgPpsM){
        if(tgPpsM.getOrgId() != null){
            if(tgPpsM.getDeptNo() != null){
                try {
                    tgPpsM.setDeptname(commonService.findDeptNameByNo(tgPpsM.getOrgId(), tgPpsM.getDeptNo(), "T"));
                } catch (Exception e) {
                }
            }
            if(tgPpsM.getPpsEmpno() != null){
                try {
                    tgPpsM.setEmpnm(commonService.findEmpnameByNo(tgPpsM.getOrgId(), tgPpsM.getPpsEmpno(), "T"));
                } catch (Exception e) {
                }
            }
        }
        return tgPpsM;
    }
}
